<?php
/**
 * Exstension for bbPress
 *
 * This file extends WPSocialStreamer to support bbPress actions
 *
 * @package WPSocialStreamer
 * @subpackage ExstensionbbPress
 */

// No direct access!
if ( !defined('ABSPATH') ) exit;

// Register needed hooks
add_action( 'bbp_new_topic', 'wpsocialstreamer_bbpress_new_topic', 10, 4);
add_action( 'bbp_new_reply', 'wpsocialstreamer_bbpress_new_reply', 10, 5);

// Add extension options filter 
add_filter( 'wpsocialstreamer_extensions', 'wpsocialstreamer_extension_bbpress_settings' );

/**
 * Extend the extension array
 * 
 * @param type $extensions_array
 * @return type
 */
function wpsocialstreamer_extension_bbpress_settings( $extensions_array ) {
  
  $extensions_array['bbpress'] = array (
    /* Set the title that will be displayed on the settings page */
    'title'     => __('bbPress', 'wpsocialstreamer'),
    /* Set a function that should be used to checked if the plugin is installed */
    'function'	=> 'bbpress',
    /* Set the link to the plugin */
    'link'		=> 'http://bbpress.org/',
    /* Extension status */
    'active' => false,
    /* Define an array with available events / actions */
    /* ATTENTION: events should be named exactly like the corresponding action! */      
    'events' => array (
      'bbp_new_topic'  => array (
        /* Set the default message template */
        'template'  => __("I've added a new topic: %POST_NAME%", 'wpsocialstreamer'),
        /* Set the event description for user settings page. */
        /* Share this event when: */
        'title'     => __("I posted a new forum topic.", 'wpsocialstreamer'),
        /* Define some placeholders that admins can use to generate message text */
        /* Set to false if no placeholders are available: 'placeholders' => false */
        'placeholders' => array ( 
          /* Set placeholder name and a short description */
          'POST_NAME'   => __("Topic name", 'wpsocialstreamer')
        ) /* END placeholders */
      ), /* END event */
      'bbp_new_reply'  => array (
        /* Set the default message template */
        'template'  => __("I just added a new reply on %POST_NAME%!", 'wpsocialstreamer'),
        /* Set the event description for user settings page. */
        /* Share this event when: */
        'title'     => __("I left a reply on a forum.", 'wpsocialstreamer'),
        /* Define some placeholders that admins can use to generate message text */
        /* Set to false if no placeholders are available: 'placeholders' => false */
        'placeholders' => array ( 
          /* Set placeholder name and a short description */
            'POST_NAME'   => __("Topic name", 'wpsocialstreamer')
        ) /* END placeholders */
      ) /* END event */
    )
  );
    
  return $extensions_array;
}

/**
 * Called when user adds a new forum topic
 * 
 * @global obj $wpss
 * @param int $topic_id
 * @return type
 */
function wpsocialstreamer_bbpress_new_topic($topic_id, $forum_id, $anonymous_data, $topic_author) {
  global $wpss;
   
  if ( empty($topic_id) ) return;
 
  // Check if this event is active
  if ( $wpss->is_active('bbpress', 'bbp_new_topic') ) {
    
    $wpss->set_raw_message( 'bbpress', 'bbp_new_topic' );
    
    // Set needed data
    $wpss->set_data( array( 
        'POST_NAME' => bbp_get_topic_title( $topic_id ),
        /* Every extension must contain the LINK field. Values: link or false */
        'LINK'      => bbp_get_topic_permalink( $topic_id ) 
      )
    );
    
    // Generate and share the message
    $wpss->generate_and_share();
  }
   
  return;
}

/**
 * Called when user adds a new forum reply
 * 
 * @global obj $wpss
 * @param int $topic_id
 * @return type
 */
function wpsocialstreamer_bbpress_new_reply($reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author) {
  global $wpss;
  
  if ( empty($topic_id) ) return;
  
  // Check if this event is active
  if ( $wpss->is_active('bbpress', 'bbp_new_topic') ) {
    
    $wpss->set_raw_message( 'bbpress', 'bbp_new_topic' );
    
    // Set needed data
    $wpss->set_data( array( 
        'POST_NAME' => bbp_get_topic_title( $topic_id ),
        /* Every extension must contain the LINK field. Values: link or false */
        'LINK'      => bbp_get_topic_permalink( $topic_id )
      )
    );
    
    // Generate and share the message
    $wpss->generate_and_share();
  }
   
  return;
}
?>